/**
 *    Copyright (C) 2011 Nadim Benabdenbi <nadim.benabdenbi@gmail.com>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.google.code.maven.plugin.beans.builder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;

public class CodeModelBuilderSourceGenerator extends AbstractBuilderSourceGenerator<JClass, JMethod, JMethod> {
	
	private JDefinedClass beanClass;
	
	protected CodeModelBuilderSourceGenerator(JCodeModel codeModel, JDefinedClass t) throws JClassAlreadyExistsException {
		super(codeModel, t);
	}
	
	@Override
	protected JClass buildBeanClass(JCodeModel codeModel, JClass t) {
		return t;
	}
	
	@Override
	protected JClass getCollectionGeneric(JMethod method) {
		JType methodParameter = getMethodParameter(method);
		if (methodParameter instanceof JClass) {
			JClass parameter = ((JClass) methodParameter).getTypeParameters().get(0);
			return parameter.getTypeParameters().get(0);
		} else {
			return null;
		}
	}
	
	@Override
	protected List<JClass> getMapGenerics(JMethod method) {
		JType methodParameter = getMethodParameter(method);
		if (methodParameter instanceof JClass) {
			List<JClass> mapGenerics = new ArrayList<JClass>();
			for (JClass parameter : ((JClass) methodParameter).getTypeParameters()) {
				mapGenerics.add(parameter.getTypeParameters().get(0));
			}
			return mapGenerics;
		} else {
			return null;
		}
	}
	
	@Override
	protected List<JClass> getConstructorExceptions(JMethod constructor) {
		return constructor.listThrowables();
	}
	
	@Override
	protected List<JType> getConstructorParameters(JMethod constructor) {
		return Arrays.asList(constructor.listParamTypes());
	}
	
	@Override
	protected List<JMethod> getConstructors() {
		List<JMethod> constructors = new ArrayList<JMethod>();
		Iterator<JMethod> constructorsIterator = beanClass.constructors();
		while (constructorsIterator.hasNext()) {
			constructors.add(constructorsIterator.next());
		}
		return constructors;
	}
	
	@Override
	protected List<JClass> getMethodExceptions(JMethod method) {
		return method.listThrowables();
	}
	
	@Override
	protected String getMethodName(JMethod method) {
		return method.name();
	}
	
	@Override
	protected JType getMethodParameter(JMethod method) {
		return method.listParamTypes()[0];
	}
	
	@Override
	protected int getMethodParameterLength(JMethod method) {
		return method.listParamTypes().length;
	}
	
	@Override
	protected List<JMethod> getMethods() {
		return Arrays.asList(beanClass.methods().toArray(new JMethod[beanClass.methods().size()]));
	}
	
	@Override
	protected boolean isConstructorPublic(JMethod constructor) {
		return (constructor.mods().getValue() & JMod.ABSTRACT) != 0;
	}
	
	@Override
	protected boolean isMethodPublic(JMethod method) {
		return (method.mods().getValue() & JMod.ABSTRACT) != 0;
	}
	
	@Override
	protected boolean isVoid(JMethod method) {
		return method.type() == codeModel.VOID;
	}
	
}
